public class PlayerLoaderSaver {
  Scanner sc = null;

  public void saveChar(Player toSave) {
    int[] stats = toSave.getStats();
    ArrayList<InvSlot> armor = toSave.getEquipped();
    ArrayList<String> abilities = toSave.getAbilities();
    ArrayList<InvSlot> inv = toSave.getInventory();
    PrintWriter writer = null;
    try {
      writer = createWriter(toSave.name+".txt");
    }
    finally {
    }

    for (int i : stats) {
      try {
        writer.print(""+i+",");
      }
      finally {
      }
    }
    for (InvSlot<Armor> i : armor) {
      try {
        String info = "," + i.getPiece().armorInfo();
        writer.print(info);
      }
      finally {
      }
    }
    try {
      writer.print(","+toSave.getMoney());
    }
    finally {
    }
    try {
      writer.print(","+abilities.size());
    }
    finally {
    }
    for (String s : abilities) {
      try {
        writer.print(","+s);
      }
      finally {
      }
    }
    try {
      writer.print(","+inv.size());
    }
    finally {
    }
    for (InvSlot i : inv) {
      try { 
        if (i.getPiece().getClass().getName().equals("sonny$Weapon")) {
          writer.print(","+1);
          String s = "," + ((Armor)(i.getPiece())).armorInfo();
          writer.print(s);
        }
        if (i.getPiece().getClass().getName().equals("sonny$Helm")) {
          writer.print(","+2);
          String s = "," + ((Armor)(i.getPiece())).armorInfo();
          writer.print(s);
        }
        if (i.getPiece().getClass().getName().equals("sonny$Body")) {
          writer.print(","+3);
          String s = "," + ((Armor)(i.getPiece())).armorInfo();
          writer.print(s);
        }
        if (i.getPiece().getClass().getName().equals("sonny$Gloves")) {
          writer.print(","+4);
          String s = "," + ((Armor)(i.getPiece())).armorInfo();
          writer.print(s);
        }
        if (i.getPiece().getClass().getName().equals("sonny$Shoes")) {
          writer.print(","+5);
          String s = "," + ((Armor)(i.getPiece())).armorInfo();
          writer.print(s);
        }
        if (i.getPiece().getClass().getName().equals("sonny$Pants")) {
          writer.print(","+6);
          String s = "," + ((Armor)(i.getPiece())).armorInfo();
          writer.print(s);
        }
        if (i.getPiece().getClass().getName().equals("sonny$Accessory")) {
          writer.print(","+7);
          String s = "," + ((Armor)(i.getPiece())).armorInfo();
          writer.print(s);
        }
      }
      finally {
      }
    }
    writer.print(",");
    writer.println();
    writer.close();
  }

  public ArrayList<Object> loadChar(String file) {
    ArrayList<Object> r = new ArrayList<Object>();
    try {
      int num;
      sc = new Scanner(createReader(file+".txt"));
      sc.useDelimiter(",");
      r.add(sc.nextInt()); //myVit
      r.add(sc.nextInt()); //myStn
      r.add(sc.nextInt()); //myIns
      r.add(sc.nextInt()); //mySpd
      r.add(sc.nextInt()); //myFoc
      r.add(sc.nextInt()); //myHP
      r.add(sc.nextInt()); //myLvl
      sc.next();
      r.add(sc.next()); //head
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.next()); //hand
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.next()); //body
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.next()); //leg
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.next()); //foot
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.next()); //accessory
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.next()); //weapon
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.nextInt());
      r.add(sc.nextInt()); //money
      num = sc.nextInt(); //numAbilities
      int a = num;
      r.add(a);
      while (num > 0) {
        r.add(sc.next());
        num--;
      }
      while (sc.hasNext ()) {
        if (sc.hasNextInt()) {
          r.add(sc.nextInt());
        }
        else {            
          r.add(sc.next());
          num--;
        }
      }
    }
    catch(Exception e) {
    }


    return r;
  }
}

